package com.imageim.yimirror.common.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;

import com.imageim.yimirror.R;
import com.imageim.yimirror.common.ui.CircleAvatarView;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;

/**
 * Created by Bruce.Zhou on 2018/6/19 17:48.
 * Email: 907160968@qq.com
 */

public class ScanFaceBottomView {

    private View root;
    private View btnBack;
    private View btnNext;
    private ImageView ivCamera;
    private CircleAvatarView caView;
    private Bitmap bitmap;
    private Context context;

    public ScanFaceBottomView(Context context, View root) {
        this.context = context;
        btnBack = root.findViewById(R.id.iv_back);
        btnNext = root.findViewById(R.id.iv_next);
        ivCamera = root.findViewById(R.id.iv_camera);
        caView = root.findViewById(R.id.ca_view);
        caView.setVisibility(View.GONE);
        btnBack.setVisibility(View.INVISIBLE);
        btnNext.setVisibility(View.INVISIBLE);
    }

    /**
     * 隐藏相机icon
     * @param bitmap
     */
    public void setAvatar(Bitmap bitmap) {
        this.bitmap = bitmap;
        hideCameraView();
        showAvatarView(bitmap);
    }

    private void hideCameraView() {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(ivCamera, "scaleX", 1, 0);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(ivCamera, "scaleY", 1, 0);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(ivCamera, "alpha", 1, 0);
        AnimatorSet anim = new AnimatorSet();
        anim.setDuration(800);
        anim.playTogether(scaleX, scaleY, alpha);
        anim.start();
    }

    private void showAvatarView(final Bitmap bitmap) {
        caView.setState(CircleAvatarView.STATE_IDLE);
        caView.setBitmap(bitmap);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(caView, "scaleX", 0, 1);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(caView, "scaleY", 0, 1);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(caView, "alpha", 0, 1);
        scaleX.setDuration(650);
        scaleY.setDuration(650);
        alpha.setDuration(650);
        AnimatorSet anim = new AnimatorSet();
        anim.setInterpolator(new AccelerateInterpolator(1f));
//        anim.addListener(new AnimatorListenerAdapter() {
//            @Override
//            public void onAnimationStart(Animator animation) {
//                  caView.setVisibility(View.VISIBLE);
//            }
//
//            @Override
//            public void onAnimationEnd(Animator animation) {
//                caView.setState(CircleAvatarView.STATE_INIT);
//                caView.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        showBtns();
//                    }
//                }, 500);
//            }
//        });
        anim.playTogether(scaleX, scaleY, alpha);
        anim.start();
        caView.setVisibility(View.VISIBLE);
    }

    private void hideAvatarView() {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(caView, "scaleX", 1, 0);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(caView, "scaleY", 1, 0);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(caView, "alpha", 1, 0);
        scaleX.setDuration(400);
        scaleY.setDuration(400);
        alpha.setDuration(400);
        AnimatorSet anim = new AnimatorSet();
        anim.setInterpolator(new AccelerateInterpolator(1f));
        anim.playTogether(scaleX, scaleY, alpha);
        anim.start();
    }

    private void showBtns() {
        btnNext.setVisibility(View.VISIBLE);
        btnBack.setVisibility(View.VISIBLE);
        showView(btnNext);
        showView(btnBack);
    }

    private void hideBtns() {
        hideView(btnBack);
        hideView(btnNext);
    }

    private void hideView(View view) {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1, 0);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1, 0);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 1, 0);
        scaleX.setDuration(600);
        scaleY.setDuration(600);
        alpha.setDuration(600);
        AnimatorSet anim = new AnimatorSet();
        anim.setInterpolator(new AccelerateInterpolator(1f));
        anim.playTogether(scaleX, scaleY, alpha);
        anim.start();
    }

    private void showView(View view) {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0, 1);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0, 1);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 0, 1);
        scaleX.setDuration(600);
        scaleY.setDuration(600);
        alpha.setDuration(600);
        AnimatorSet anim = new AnimatorSet();
        anim.setInterpolator(new AccelerateInterpolator(1f));
        anim.playTogether(scaleX, scaleY, alpha);
        anim.start();
    }

    private void showCameraView() {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(ivCamera, "scaleX", 0, 1);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(ivCamera, "scaleY", 0, 1);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(ivCamera, "alpha", 0, 1);
        AnimatorSet anim = new AnimatorSet();
        anim.setDuration(1000);
        anim.playTogether(scaleX, scaleY, alpha);
        anim.start();
    }

    /**
     * 显示相机icon
     */
    public void reset() {
        showCameraView();
        hideBtns();
        hideAvatarView();
    }

    public void startUploding() {
        caView.setState(CircleAvatarView.STATE_RUN);
    }

    public void stopUploding() {
        caView.setState(CircleAvatarView.STATE_INIT);
    }

    public void setState(int state) {
        caView.setState(state);
    }

    public void setOnBackClickListener(View.OnClickListener listener) {
        btnBack.setOnClickListener(listener);
    }

    public void setOnNextClickListener(View.OnClickListener listener) {
        btnNext.setOnClickListener(listener);
    }
}
